package com.twu.biblioteca;


public class BibliotecaApp
{
    static Library library;
    static ReadUserInput userInput;
    String currentUser;

       public BibliotecaApp()
       {
           System.out.println("Welcome to Biblioteca Release 1");

           library = new Library();
           userInput = new ReadUserInput();
           UserOptions.initOptions();
       }

       public Boolean login()
       {
           System.out.println("Enter your username --");
           currentUser = userInput.getUserInput();

           for(int i = 0; i < Library.users.length; i++)
           {
               if(Library.users[i].username.equals(currentUser))
               {
                   System.out.println("Enter your password ---");

                   if(Library.users[i].password.equals(userInput.getUserInput()))
                   {
                       System.out.println("You are successfully logged in!");
                       Library.users[i].loginFlag = true;
                       return true;
                   }
                   else
                   {
                       System.out.println("Incorrect password!");
                       break;
                   }

               }
           }
           System.out.println("Username does not exist, please try logging in again!");
           return false;
       }


        boolean isUserLoggedIn()
        {
            if(this.currentUser != null)
            {
                for(int i = 0; i < Library.users.length; i++)
                {
                    if(Library.users[i].username.equals(currentUser))
                    {
                        if(Library.users[i].loginFlag.equals(true))
                            return true;
                        else
                            break;
                    }
                }
            }
            return false;
        }

        public static void main(String[] args)
        {
            BibliotecaApp app = new BibliotecaApp();
            UserOptions.showAvailableOptions();


            while(true)
            {
                System.out.println("Select one of the available options by entering the number against it...");
                switch ((Integer.parseInt(userInput.getUserInput())))
                {
                    case 1:app.login();
                           break;

                    case 2:System.out.println("List of all books in the library --");
                           Library.listAllBooks();
                           break;

                    case 3:System.out.println("Enter the book number to reserve a book --");
                           library.reserveBook((Integer.parseInt(userInput.getUserInput())), app.currentUser, app.isUserLoggedIn());
                           break;

                    case 4:library.getUserMembershipDetails(app.currentUser, app.isUserLoggedIn());
                           break;

                    case 5:System.out.println("Details of all movies in the library --");
                           Library.showAllMovies();
                           break;

                    case 6:System.exit(0);

                    default:System.out.println("Please enter a valid option");
                }
            }

       }
}
